home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / PROGRAMS / CH12 / CREATEDB.TXT < prev    next >
Encoding:
Text File  |  1998-09-21  |  2.8 KB  |  90 lines

  1. Dim MyDB As Database, MyWs As Workspace
  2. Dim T1, T2 As TableDef
  3. Dim T1Flds(1 To 2), T2Flds(1 To 3) As Field
  4. Dim TempFld As Field
  5. Dim T1Idx, T2Idx As Index
  6. Dim Rel As Relation
  7. Dim MyRec As Recordset
  8. 'Create Database
  9. Set MyWs = DBEngine.Workspaces(0)
  10. Set MyDB = MyWs.CreateDatabase("C:\DBNAME.MDB", dbLangGeneral)
  11. Rem Create first Table, TABLE1
  12. Set T1 = MyDB.CreateTableDef("TABLE1")
  13. 'Specify fields for TABLE1
  14. 'Note the use of the optional parameter 50 for field size
  15. 'If 50 is omitted, the size will default to 20
  16. Set T1Flds(1) = T1.CreateField("FIELD1A", dbText, 50)
  17. Set T1Flds(2) = T1.CreateField("FIELD1B", dbSingle)
  18. 'Add the New fields to the field list in the Table
  19. T1.Fields.Append T1Flds(1)
  20. T1.Fields.Append T1Flds(2)
  21. 'Specify a primary field for TABLE1
  22. Set T1Idx = T1.CreateIndex("FIELD1A")
  23. T1Idx.Primary = True
  24. T1Idx.Unique = True
  25. T1Idx.Required = True
  26. Set T1Flds(1) = T1Idx.CreateField("FIELD1A")
  27. 'Add this field to the field list of the Index
  28. T1Idx.Fields.Append T1Flds(1)
  29. 'Add this Index to the index list of the Table
  30. T1.Indexes.Append T1Idx
  31. 'Add the Table to the Database
  32. MyDB.TableDefs.Append T1
  33. 'Create TABLE2
  34. Set T2 = MyDB.CreateTableDef("TABLE2")
  35. 'Specify fields for TABLE2
  36. Set T2Flds(1) = T2.CreateField("FIELD2A", dbText, 50)
  37. Set T2Flds(2) = T2.CreateField("FIELD2B", dbSingle)
  38. Set T2Flds(3) = T2.CreateField("FIELD2C", dbInteger)
  39. 'Add the new fields to the field list of the Table
  40. T2.Fields.Append T2Flds(1)
  41. T2.Fields.Append T2Flds(2)
  42. T2.Fields.Append T2Flds(3)
  43. 'Set the primary field for TABLE2
  44. Set T2Idx = T2.CreateIndex("FIELD2C")
  45. T2Idx.Primary = True
  46. T2Idx.Unique = True
  47. T2Idx.Required = True
  48. Set T2Flds(3) = T2Idx.CreateField("FIELD2C")
  49. 'Add this field to the field list of the Index
  50. T2Idx.Fields.Append T2Flds(3)
  51. 'Add this index to the index list of TABLE2
  52. T2.Indexes.Append T2Idx
  53. 'Add TABLE2 to the Database
  54. MyDB.TableDefs.Append T2
  55. 'Set up the relation between the tables
  56. Set Rel = MyDB.CreateRelation("foreign", "TABLE1", "TABLE2")
  57. Rel.Attributes = 0
  58. 'Mark the primary field in TABLE1
  59. Set T2Flds(1) = Rel.CreateField("FIELD1A")
  60. 'Mark the foreign key field in TABLE2
  61. T2Flds(1).ForeignName = "FIELD2A"
  62. 'Add the field to the field list of the relation
  63. Rel.Fields.Append T2Flds(1)
  64. 'Add the relation to the database
  65. MyDB.Relations.Append Rel
  66. 'Add a record to each table
  67. 'Open a recordset referring to TABLE1
  68. Set MyRec = T1.OpenRecordset
  69. 'Create a record
  70. MyRec.AddNew
  71. MyRec("FIELD1A") = "alpha"
  72. MyRec("FIELD1B") = 1997
  73. 'Update the recordset
  74. MyRec.Update
  75. 'Close the recordset referring to TABLE1
  76. MyRec.Close
  77. 'Open a recordset referring to TABLE2
  78. Set MyRec = T2.OpenRecordset
  79. 'Create a record
  80. MyRec.AddNew
  81. MyRec("FIELD2A") = "alpha"
  82. MyRec("FIELD2B") = 2000
  83. MyRec("FIELD2C") = 1
  84. 'Update the recordset
  85. MyRec.Update
  86. 'Close the recordset
  87. MyRec.Close
  88. 'Close the database
  89. MyDB.Close
  90.